home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Suzy B Software 2
/
Suzy B Software CD-ROM 2 (1994).iso
/
extras
/
programm
/
graphic
/
graphics.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-04-27
|
37KB
|
1,483 lines
#include <stdio.h>
#include <tos.h>
#define G_REPLACE 1
#define G_AND 2
#define G_XOR 3
#define G_NOT 4
#define G_OR 5
typedef struct
{
unsigned int red:5;
unsigned int green:5;
unsigned int overlay:1;
unsigned int blue:5;
}HIGHCOLOR;
int color_shift;
int _ybase, _xbase;
int _circle_color;
int _max_x, _max_y;
unsigned long _aspect;
float _monitor_aspect = 0.75; /* Aspect ratio of your monitor. */
long *_ytable;
long *_scale;
long *_scaleptr;
int _write_mode = G_REPLACE;
/* ------------------------------------------------------------------- */
/* Function Prototype */
/* ------------------------------------------------------------------- */
int (*p_pixel)(int x, int y, int color);
int (*g_pixel)(int x, int y);
int (*d_line)(int x1, int y1, int x2, int y2, int color);
int (*d_hline)(int x1, int x2, int y, int color);
int (*byte2raw)(char *colors, char *buffer);
int pinit(int max_x, int max_y, void *video_buffer, int planes);
void pexit(void);
int set_pixel1(int x, int y, int color);
int set_pixel2(int x, int y, int color);
int set_pixel4(int x, int y, int color);
int set_pixel8(int x, int y, int color);
int set_pixel16(int x, int y, int color);
int get_pixel1(int x, int y);
int get_pixel2(int x, int y);
int get_pixel4(int x, int y);
int get_pixel8(int x, int y);
int get_pixel16(int x, int y);
extern int byte2raw8(char *color, char *buffer);
extern int byte2raw4(char *color, char *buffer);
extern int byte2raw2(char *color, char *buffer);
extern int byte2raw1(char *color, char *buffer);
void draw_line1(int x1, int y1, int x2, int y2, int color);
void draw_line2(int x1, int y1, int x2, int y2, int color);
void draw_line4(int x1, int y1, int x2, int y2, int color);
void draw_line8(int x1, int y1, int x2, int y2, int color);
void draw_line16(int x1, int y1, int x2, int y2, int color);
void draw_hline1(int x1, int x2, int y, int color);
void draw_hline2(int x1, int x2, int y, int color);
void draw_hline4(int x1, int x2, int y, int color);
void draw_hline8(int x1, int x2, int y, int color);
void draw_hline16(int x1, int x2, int y, int color);
void set_colors(void);
void set_writemode(int mode);
void _plot8(int x, int y);
void _plot(int x, int y);
void _plotfill(int x1, int x2, int y);
void circle(int x, int y, int radius, int color);
void disk(int x, int y, int radius, int color);
/****************************************************************/
/* Set the write mode for graphics operations. */
/****************************************************************/
void set_writemode(int mode)
{
switch(mode)
{
case G_REPLACE:
case G_AND:
case G_XOR:
case G_NOT:
case G_OR: _write_mode = mode;
break;
default: _write_mode = G_REPLACE;
break;
}
}
/****************************************************************/
/* Draw a circle at x,y,radius,color */
/****************************************************************/
void circle(int x, int y, int radius, int color)
{
register int xx, yy, sum, sx, sy;
_xbase = x;
_ybase = y;
_circle_color = color;
xx = 0;
yy = radius << 1;
sum = 0;
while(xx-1 <= yy) {
if(!(xx & 1))
{
sx = xx >> 1;
sy = (yy+1) >> 1;
_plot(sx, sy);
_plot(-sx, sy);
_plot(sx,-sy);
_plot(-sx, -sy);
_plot(sy, sx);
_plot(-sy, sx);
_plot(sy, -sx);
_plot(-sy, -sx);
}
sum += (xx << 1) + 1;
xx++;
if(sum > 0) {
sum -= (yy << 1) - 1;
yy--;
}
}
}
/****************************************************************/
/* Draw a disk at x,y,radius,color */
/****************************************************************/
void disk(int x, int y, int radius, int color)
{
register int xx, yy, sum, sx, sy;
_xbase = x;
_ybase = y;
_circle_color = color;
xx = 0;
yy = radius << 1;
sum = 0;
while(xx-1 <= yy) {
if(!(xx & 1))
{
sx = xx >> 1;
sy = (yy+1) >> 1;
_plotfill(sx, -sx, sy);
_plotfill(sx, -sx, -sy);
_plotfill(sy, -sy, sx);
_plotfill(sy, -sy, -sx);
}
sum += (xx << 1) + 1;
xx++;
if(sum > 0) {
sum -= (yy << 1) - 1;
yy--;
}
}
}
/****************************************************************/
/* Plot each circle pixel. */
/****************************************************************/
void _plotfill(int x1, int x2, int y)
{
register int x1_pos, x2_pos, y_pos;
if(_aspect == 0) {
x1_pos = x1 + _xbase;
x2_pos = x2 + _xbase;
y_pos = y + _ybase;
}
else {
x1_pos = _scale[x1] + _xbase;
x2_pos = _scale[x2] + _xbase;
y_pos = y + _ybase;
}
if((x1_pos < 0) && (x2_pos < 0)) return;
if((x1_pos > _max_x) && (x2_pos > _max_x)) return;
if(y_pos < 0) return;
if(y_pos > _max_y) return;
if(x1_pos < 0) x1_pos = 0;
if(x1_pos > _max_x) x1_pos = _max_x;
if(x2_pos < 0) x2_pos = 0;
if(x2_pos > _max_x) x2_pos = _max_x;
(*d_hline)(x1_pos, x2_pos, y_pos, _circle_color);
}
/****************************************************************/
/* Plot each circle pixel. */
/****************************************************************/
void _plot(register int x, register int y)
{
register int x_pos, y_pos;
if(_aspect == 0) {
x_pos = x + _xbase;
y_pos = y + _ybase;
}
else {
x_pos = _scale[x] + _xbase;
y_pos = y + _ybase;
}
if(x_pos < 0) return;
if(y_pos < 0) return;
if(x_pos > _max_x) return;
if(y_pos > _max_y) return;
(*p_pixel)(x_pos, y_pos, _circle_color);
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_hline1(int x1, int x2, int y, int color)
{
register char *video_buffer;
register char *end_line;
register char on_mask;
register char off_mask;
register int index;
register int b_index;
if(x1 < 0) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 0) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y > _max_y) y = _max_y;
if(y < 0) y = 0;
if(x1 > x2) {
b_index = x1;
x1 = x2;
x2 = b_index;
}
b_index = (x2 >> 3) - (x1 >> 3) - 1;
if(b_index < 1) {
(*d_line)(x1, y, x2, y, color);
return;
}
video_buffer = (char *)(_ytable[y] + (x1 >> 3));
on_mask = 0xff;
on_mask >>= (x1 & 0x7);
off_mask = ~on_mask;
*video_buffer &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
end_line = (char *)(_ytable[y] + (x2 >> 3));
on_mask = 0xff;
on_mask >>= (x2 & 0x7);
off_mask = ~on_mask;
*end_line &= on_mask;
if(color & 0x1)
*end_line |= off_mask;
on_mask = 0xff;
off_mask = ~on_mask;
for(index = 0; index < b_index; index++) {
video_buffer++;
*video_buffer &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_hline2(int x1, int x2, int y, int color)
{
register int *video_buffer;
register int *end_line;
register int on_mask;
register int off_mask;
register long loff_mask;
register int index;
register int b_index;
if(x1 < 0) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 0) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y > _max_y) y = _max_y;
if(y < 0) y = 0;
if(x1 > x2) {
b_index = x1;
x1 = x2;
x2 = b_index;
}
b_index = (x2 >> 4) - (x1 >> 4) - 1;
if(b_index < 1) {
(*d_line)(x1, y, x2, y, color);
return;
}
video_buffer = (int *)((int *)_ytable[y] + ((x1 >> 3) & 0xfffe));
on_mask = 0xFFFF >> (x1 & 0xf);
off_mask = ~on_mask;
*(video_buffer) &= (int) off_mask;
*(video_buffer+1) &= (int) off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
if(color & 0x2)
*(video_buffer+1) |= on_mask;
end_line = (int *)((int *)_ytable[y] + ((x2 >> 3) & 0xfffe));
on_mask = 0xFFFF >> (x2 & 0xf);
off_mask = ~on_mask;
*(end_line) &= on_mask;
*(end_line+1) &= on_mask;
if(color & 0x1)
*end_line |= off_mask;
if(color & 0x2)
*(end_line+1) |= off_mask;
on_mask = 0xffff;
loff_mask = 0x0L;
for(index = 0; index < b_index; index++) {
video_buffer += 2;
*((long *)video_buffer) &= loff_mask;
if(color & 0x1) *video_buffer |= on_mask;
if(color & 0x2) *(video_buffer+1) |= on_mask;
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_hline4(int x1, int x2, int y, int color)
{
register int *video_buffer;
register int *end_line;
register int on_mask;
register int off_mask;
register int loff_mask;
register int index;
register int b_index;
if(x1 < 0) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 0) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y > _max_y) y = _max_y;
if(y < 0) y = 0;
if(x1 > x2) {
b_index = x1;
x1 = x2;
x2 = b_index;
}
b_index = (x2 >> 4) - (x1 >> 4) - 1;
if(b_index < 1) {
(*d_line)(x1, y, x2, y, color);
return;
}
video_buffer = (int *)((int *)_ytable[y] + ((x1 >> 2) & 0xfffc));
on_mask = 0xFFFF >> (x1 & 0xf);
off_mask = ~on_mask;
*(video_buffer) &= off_mask;
*(video_buffer+1) &= off_mask;
*(video_buffer+2) &= off_mask;
*(video_buffer+3) &= off_mask;
if(color & 0x1) *video_buffer |= on_mask;
if(color & 0x2) *(video_buffer+1) |= on_mask;
if(color & 0x4) *(video_buffer+2) |= on_mask;
if(color & 0x8) *(video_buffer+3) |= on_mask;
end_line = (int *)((int *)_ytable[y] + ((x2 >> 2) & 0xfffc));
on_mask = 0xFFFF >> (x2 & 0xf);
off_mask = ~on_mask;
*(end_line) &= on_mask;
*(end_line+1) &= on_mask;
*(end_line+2) &= on_mask;
*(end_line+3) &= on_mask;
if(color & 0x1) *end_line |= off_mask;
if(color & 0x2) *(end_line+1) |= off_mask;
if(color & 0x4) *(end_line+2) |= off_mask;
if(color & 0x8) *(end_line+3) |= off_mask;
on_mask = 0xfffff;
loff_mask = 0x0L;
for(index = 0; index < b_index; index++) {
video_buffer += 4;
*((long *)video_buffer) &= loff_mask;
*((long *)video_buffer+1) &= loff_mask;
if(color & 0x1) *video_buffer |= on_mask;
if(color & 0x2) *(video_buffer+1) |= on_mask;
if(color & 0x4) *(video_buffer+2) |= on_mask;
if(color & 0x8) *(video_buffer+3) |= on_mask;
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_hline8(int x1, int x2, int y, int color)
{
register int *video_buffer;
register int *end_line;
register int on_mask;
register int off_mask;
register int loff_mask;
register int index;
register int b_index;
if(x1 < 0) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 0) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y > _max_y) y = _max_y;
if(y < 0) y = 0;
if(x1 > x2) {
b_index = x1;
x1 = x2;
x2 = b_index;
}
b_index = (x2 >> 4) - (x1 >> 4) - 1;
if(b_index < 1) {
(*d_line)(x1, y, x2, y, color);
return;
}
video_buffer = (int *)((int *)_ytable[y] + ((x1 >> 1) & 0xfff8));
on_mask = 0xFFFF >> (x1 & 0xf);
off_mask = ~on_mask;
*(video_buffer) &= off_mask;
*(video_buffer+1) &= off_mask;
*(video_buffer+2) &= off_mask;
*(video_buffer+3) &= off_mask;
*(video_buffer+4) &= off_mask;
*(video_buffer+5) &= off_mask;
*(video_buffer+6) &= off_mask;
*(video_buffer+7) &= off_mask;
if(color & 0x1) *video_buffer |= on_mask;
if(color & 0x2) *(video_buffer+1) |= on_mask;
if(color & 0x4) *(video_buffer+2) |= on_mask;
if(color & 0x8) *(video_buffer+3) |= on_mask;
if(color & 0x10) *(video_buffer+4) |= on_mask;
if(color & 0x20) *(video_buffer+5) |= on_mask;
if(color & 0x40) *(video_buffer+6) |= on_mask;
if(color & 0x80) *(video_buffer+7) |= on_mask;
end_line = (int *)((int *)_ytable[y] + ((x2 >> 1) & 0xfff8));
on_mask = 0xFFFF >> (x2 & 0xf);
off_mask = ~on_mask;
*(end_line) &= on_mask;
*(end_line+1) &= on_mask;
*(end_line+2) &= on_mask;
*(end_line+3) &= on_mask;
*(end_line+4) &= on_mask;
*(end_line+5) &= on_mask;
*(end_line+6) &= on_mask;
*(end_line+7) &= on_mask;
if(color & 0x1) *end_line |= off_mask;
if(color & 0x2) *(end_line+1) |= off_mask;
if(color & 0x4) *(end_line+2) |= off_mask;
if(color & 0x8) *(end_line+3) |= off_mask;
if(color & 0x10) *(end_line+4) |= off_mask;
if(color & 0x20) *(end_line+5) |= off_mask;
if(color & 0x40) *(end_line+6) |= off_mask;
if(color & 0x80) *(end_line+7) |= off_mask;
on_mask = 0xfffff;
loff_mask = 0x0L;
for(index = 0; index < b_index; index++) {
video_buffer += 8;
*((long *)video_buffer) &= loff_mask;
*((long *)video_buffer+1) &= loff_mask;
*((long *)video_buffer+2) &= loff_mask;
*((long *)video_buffer+3) &= loff_mask;
if(color & 0x1) *video_buffer |= on_mask;
if(color & 0x2) *(video_buffer+1) |= on_mask;
if(color & 0x4) *(video_buffer+2) |= on_mask;
if(color & 0x8) *(video_buffer+3) |= on_mask;
if(color & 0x10) *(video_buffer+4) |= on_mask;
if(color & 0x20) *(video_buffer+5) |= on_mask;
if(color & 0x40) *(video_buffer+6) |= on_mask;
if(color & 0x80) *(video_buffer+7) |= on_mask;
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_hline16(int x1, int x2, int y, int color)
{
register int *video_buffer;
register int b_index;
register int index;
if(x1 < 0) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 0) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y > _max_y) y = _max_y;
if(y < 0) y = 0;
if(x1 > x2) {
b_index = x1;
x1 = x2;
x2 = b_index;
}
b_index = x2 - x1;
video_buffer = ((int *) _ytable[y]) + x1;
for(index = 0; index < b_index; index++)
*video_buffer++ = color;
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_line1(int x1, int y1, int x2, int y2, int color)
{
#define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
register int x, y, py, px;
register int dx,dy,dxabs,dyabs,i,sdx,sdy;
register char *video_buffer;
register char on_mask;
register char off_mask;
if(x1 < 1) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 1) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y1 > _max_y) y1 = _max_y;
if(y1 < 1) y1 = 0;
if(y2 > _max_y) y2 = _max_y;
if(y2 < 1) y2 = 0;
dx = x2 - x1;
dy = y2 - y1;
sdx = sign(dx);
sdy = sign(dy);
dxabs = abs(dx);
dyabs = abs(dy);
x = 0;
y = 0;
px = x1;
py = y1;
if(dxabs >= dyabs)
{
for(i = 0; i <= dxabs; i++)
{
y += dyabs;
if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}
video_buffer = (char *)(_ytable[py] + (px >> 3));
on_mask = 0x80;
on_mask >>= (px & 0x7);
off_mask = ~on_mask;
*video_buffer &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
px += sdx;
}
}
else
{
for(i = 0; i < dyabs; i++)
{
x += dxabs;
if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
video_buffer = (char *)(_ytable[py] + (px >> 3));
on_mask = 0x80;
on_mask >>= (px & 0x7);
off_mask = ~on_mask;
*video_buffer &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
py += sdy;
}
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_line2(int x1, int y1, int x2, int y2, int color)
{
#define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
register int x, y, py, px;
register int dx,dy,dxabs,dyabs,i,sdx,sdy;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x1 < 1) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 1) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y1 > _max_y) y1 = _max_y;
if(y1 < 1) y1 = 0;
if(y2 > _max_y) y2 = _max_y;
if(y2 < 1) y2 = 0;
dx = x2 - x1;
dy = y2 - y1;
sdx = sign(dx);
sdy = sign(dy);
dxabs = abs(dx);
dyabs = abs(dy);
x = 0;
y = 0;
px = x1;
py = y1;
if(dxabs >= dyabs)
{
for(i = 0; i <= dxabs; i++)
{
y += dyabs;
if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 3) & 0xfffe));
*((long *)video_buffer) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
px += sdx;
}
}
else
{
for(i = 0; i < dyabs; i++)
{
x += dxabs;
if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 3) & 0xfffe));
*((long *)video_buffer) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
py += sdy;
}
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_line4(int x1, int y1, int x2, int y2, int color)
{
#define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
register int x, y, py, px;
register int dx,dy,dxabs,dyabs,i,sdx,sdy;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x1 < 1) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 1) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y1 > _max_y) y1 = _max_y;
if(y1 < 1) y1 = 0;
if(y2 > _max_y) y2 = _max_y;
if(y2 < 1) y2 = 0;
dx = x2 - x1;
dy = y2 - y1;
sdx = sign(dx);
sdy = sign(dy);
dxabs = abs(dx);
dyabs = abs(dy);
x = 0;
y = 0;
px = x1;
py = y1;
if(dxabs >= dyabs)
{
for(i = 0; i <= dxabs; i++)
{
y += dyabs;
if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 2) & 0xfffc));
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
px += sdx;
}
}
else
{
for(i = 0; i < dyabs; i++)
{
x += dxabs;
if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 2) & 0xfffc));
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
py += sdy;
}
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_line8(int x1, int y1, int x2, int y2, int color)
{
#define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
register int x, y, py, px;
register int dx,dy,dxabs,dyabs,i,sdx,sdy;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x1 < 1) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 1) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y1 > _max_y) y1 = _max_y;
if(y1 < 1) y1 = 0;
if(y2 > _max_y) y2 = _max_y;
if(y2 < 1) y2 = 0;
dx = x2 - x1;
dy = y2 - y1;
sdx = sign(dx);
sdy = sign(dy);
dxabs = abs(dx);
dyabs = abs(dy);
x = 0;
y = 0;
px = x1;
py = y1;
if(dxabs >= dyabs)
{
for(i = 0; i <= dxabs; i++)
{
y += dyabs;
if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 1) & 0xfff8));
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
*((long *)video_buffer+2) &= off_mask;
*((long *)video_buffer+3) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x10)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x20)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x40)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x80)
*video_buffer |= on_mask;
px += sdx;
}
}
else
{
for(i = 0; i < dyabs; i++)
{
x += dxabs;
if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
off_mask = ~(0x80008000L >> (px & 0xf));
on_mask = 0x8000 >> (px & 0xf);
video_buffer = (int *)((int *)_ytable[py] + ((px >> 1) & 0xfff8));
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
*((long *)video_buffer+2) &= off_mask;
*((long *)video_buffer+3) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x10)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x20)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x40)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x80)
*video_buffer |= on_mask;
py += sdy;
}
}
}
/****************************************************************/
/* Draw a line x1,y1,x2,y2,color */
/****************************************************************/
void draw_line16(int x1, int y1, int x2, int y2, int color)
{
#define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
register int x, y, py, px;
register int dx,dy,dxabs,dyabs,i,sdx,sdy;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x1 < 1) x1 = 0;
if(x1 > _max_x) x1 = _max_x;
if(x2 < 1) x2 = 0;
if(x2 > _max_x) x2 = _max_x;
if(y1 > _max_y) y1 = _max_y;
if(y1 < 1) y1 = 0;
if(y2 > _max_y) y2 = _max_y;
if(y2 < 1) y2 = 0;
dx = x2 - x1;
dy = y2 - y1;
sdx = sign(dx);
sdy = sign(dy);
dxabs = abs(dx);
dyabs = abs(dy);
x = 0;
y = 0;
px = x1;
py = y1;
if(dxabs >= dyabs)
{
for(i = 0; i <= dxabs; i++)
{
y += dyabs;
if(y >= dxabs)
{
y -= dxabs;
py += sdy;
}
*(unsigned int *) (((int *)_ytable[py]) + (px)) = color;
px += sdx;
}
}
else
{
for(i = 0; i < dyabs; i++)
{
x += dxabs;
if(x >= dyabs)
{
x -= dyabs;
px += sdx;
}
*(unsigned int *) (((int *)_ytable[py]) + (px)) = color;
py += sdy;
}
}
}
/*************************************************************/
pinit(int max_x, int max_y, void *video_buffer, int planes)
{
int offset = 0;
int index = 0;
int i = 0;
long tempcount = 0;
char *temp;
float ftemp;
static char *errmsg = "Graphics Memory Allocation Error!";
_max_x = max_x-1;
_max_y = max_y-1;
if(Malloc(-1) < sizeof(long) * max_y) {
fprintf(stderr,"%s\n",errmsg);
return(-1);
}
_ytable = Malloc((long)sizeof(long) * (long)max_y);
temp = (char *)video_buffer;
/* Get the number of byte per X row. */
offset = (max_x / 8) * planes;
/* Build the Y vector table. */
for(index = 0; index < max_y; index++) {
*(_ytable+index) = temp;
temp += offset;
}
/* Calulate screen aspect ratio for circle drawing routine. */
if(_monitor_aspect == 0.0) _monitor_aspect = 0.75;
ftemp = ((float) max_y) / ((float) max_x) / _monitor_aspect;
if(ftemp < 0.0) ftemp = -ftemp;
_aspect = 0;
if(ftemp != 1.0) {
tempcount = (long) sizeof(long) * (long) (max_x*2) * 2;
tempcount += sizeof(long) * 2;
if(Malloc(-1) < tempcount) {
fprintf(stderr,"%s\n",errmsg);
return(-1);
}
_scaleptr = Malloc(tempcount);
if(max_y > max_x)
ftemp = ((float) max_x) / ((float) max_y) / (1 / _monitor_aspect);
_aspect = 1;
i = 0;
for(index = -max_x; index <= max_x; index++) {
*(_scaleptr+i) = (long)(((long) index) * ftemp);
i++;
if(index == 0) _scale = _scaleptr+i;
}
}
switch(planes)
{
default: p_pixel = set_pixel1;
d_line = draw_line1;
d_hline = draw_hline1;
g_pixel = get_pixel1;
byte2raw = byte2raw1;
color_shift = 7;
break;
case 2: p_pixel = set_pixel2;
d_line = draw_line2;
d_hline = draw_hline2;
g_pixel = get_pixel2;
byte2raw = byte2raw2;
color_shift = 6;
break;
case 4: p_pixel = set_pixel4;
d_line = draw_line4;
d_hline = draw_hline4;
g_pixel = get_pixel4;
byte2raw = byte2raw4;
color_shift = 4;
break;
case 8: p_pixel = set_pixel8;
d_line = draw_line8;
d_hline = draw_hline8;
g_pixel = get_pixel8;
byte2raw = byte2raw8;
color_shift = 0;
break;
case 16: p_pixel = set_pixel16;
d_line = draw_line16;
d_hline = draw_hline16;
g_pixel = get_pixel16;
byte2raw = byte2raw8;
color_shift = 0;
set_colors();
break;
}
}
/*************************************************************/
void pexit()
{
Mfree(_ytable);
Mfree(_scaleptr);
}
/*************************************************************/
set_pixel1(register int x, register int y, int color)
{
register int xx;
register unsigned char *video_buffer;
register unsigned char on_mask;
register unsigned char off_mask;
if(x < 0 || x > _max_x) return;
if(y < 0 || y > _max_y) return;
xx = x >> 3;
x &= 0x7;
video_buffer = (char *)(_ytable[y] + xx);
on_mask = 0x80;
on_mask >>= x;
off_mask = ~on_mask;
*video_buffer &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
}
/*************************************************************/
set_pixel2(register int x, register int y, int color)
{
register int xx;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x < 0 || x > _max_x) return;
if(y < 0 || y > _max_y) return;
xx = (x >> 4);
xx <<= 1;
x &= 0xf;
off_mask = ~(0x80008000L >> x);
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
*((long *)video_buffer) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
}
/*************************************************************/
set_pixel4(register int x, register int y, int color)
{
register int xx;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x < 0 || x > _max_x) return;
if(y < 0 || y > _max_y) return;
xx = (x >> 4);
xx <<= 2;
x &= 0xf;
off_mask = ~(0x80008000L >> x);
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
}
/*************************************************************/
set_pixel8(register int x, register int y, int color)
{
register int xx;
register int *video_buffer;
register int on_mask;
register long off_mask;
if(x < 0 || x > _max_x) return;
if(y < 0 || y > _max_y) return;
xx = (x >> 4);
xx <<= 3;
x &= 0xf;
off_mask = ~(0x80008000L >> x);
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
*((long *)video_buffer) &= off_mask;
*((long *)video_buffer+1) &= off_mask;
*((long *)video_buffer+2) &= off_mask;
*((long *)video_buffer+3) &= off_mask;
if(color & 0x1)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x2)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x4)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x8)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x10)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x20)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x40)
*video_buffer |= on_mask;
video_buffer++;
if(color & 0x80)
*video_buffer |= on_mask;
}
/*************************************************************/
set_pixel16(int x, int y, int color)
{
if(x < 0 || x > _max_x) return;
if(y < 0 || y > _max_y) return;
*(unsigned int *) (((int *)_ytable[y]) + (x)) = color;
}
/****************************************************************/
int get_pixel1(int x, int y)
{
register int xx;
register char *video_buffer;
register char on_mask;
register int color;
if(x < 0 || x > _max_x) return(1);
if(y < 0 || y > _max_y) return(1);
xx = x >> 3;
x &= 0x7;
video_buffer = (char *)(_ytable[y] + xx);
on_mask = 0x80;
on_mask >>= x;
color = 0;
if(*video_buffer & on_mask) color |= 0x1;
return(color);
}
/****************************************************************/
int get_pixel2(int x, int y)
{
register int xx;
register int *video_buffer;
register int on_mask;
register int color;
if(x < 0 || x > _max_x) return(1);
if(y < 0 || y > _max_y) return(1);
xx = (x >> 4);
xx <<= 1;
x &= 0xf;
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
color = 0;
if(*video_buffer & on_mask) color |= 0x1;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x2;
return(color);
}
/****************************************************************/
int get_pixel4(int x, int y)
{
register int xx;
register int *video_buffer;
register int on_mask;
register int color;
if(x < 0 || x > _max_x) return(1);
if(y < 0 || y > _max_y) return(1);
xx = (x >> 4);
xx <<= 2;
x &= 0xf;
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
color = 0;
if(*video_buffer & on_mask) color |= 0x1;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x2;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x4;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x8;
return(color);
}
/****************************************************************/
int get_pixel8(int x, int y)
{
register int xx;
register int *video_buffer;
register int on_mask;
register int color;
if(x < 0 || x > _max_x) return(1);
if(y < 0 || y > _max_y) return(1);
xx = (x >> 4);
xx <<= 3;
x &= 0xf;
on_mask = 0x8000 >> x;
video_buffer = (int *)((int *)_ytable[y] + xx);
color = 0;
if(*video_buffer & on_mask) color |= 0x1;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x2;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x4;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x8;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x10;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x20;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x40;
video_buffer++;
if(*video_buffer & on_mask) color |= 0x80;
video_buffer++;
return(color);
}
/*************************************************************/
int get_pixel16(int x, int y)
{
return(*(unsigned int *) (((int *)_ytable[y]) + (x)));
}